# 给你一个正整数 n ，生成一个包含 1 到 n² 所有元素，且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。 
# 
#  
# 
#  示例 1： 
#  
#  
# 输入：n = 3
# 输出：[[1,2,3],[8,9,4],[7,6,5]]
#  
# 
#  示例 2： 
# 
#  
# 输入：n = 1
# 输出：[[1]]
#  
# 
#  
# 
#  提示： 
# 
#  
#  1 <= n <= 20 
#  
# 
#  Related Topics 数组 矩阵 模拟 👍 1356 👎 0
from typing import List


# leetcode submit region begin(Prohibit modification and deletion)
class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        arr = [[0]*n for _ in range(n)]
        top = 0
        bot = n-1
        left = 0
        right = n-1
        i = 0
        j = 0
        num = 1
        arr[0][0] = 1
        first = 1
        while top <= bot and left<=right:
            for j in range(left,right+1):
                arr[i][j]= num
                num+=1
            top+=1

            for i in range(top,bot+1):
                arr[i][j] = num
                num+=1
            right-=1

            for j in range(right,left-1,-1):
                arr[i][j] = num
                num+=1
            bot-=1

            for i in range(bot,top-1,-1):
                arr[i][j] = num
                num += 1
            left += 1

        return arr



# leetcode submit region end(Prohibit modification and deletion)
Solution().generateMatrix(3)